package com.platform.task;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

@Component("weatherTask")
public class WeatherTask {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 天气实况信息
     */
    public void weatherObserve() throws IOException {
        URL url = new URL("http://api.weatherdt.com/common/?area=101120308&type=observe&key=5b442752f46cc6cd3c51ece9ae48e162");
        HttpURLConnection uRLConnection = (HttpURLConnection)url.openConnection();
        uRLConnection.setDoOutput(true);
        uRLConnection.connect();
        InputStream is = uRLConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String respJson = "";
        String readLine = null;
        while((readLine =br.readLine()) != null){
            respJson +=readLine;
        }
        JSONObject jsonObject = JSONObject.parseObject(respJson).getJSONObject("observe").getJSONObject("101120308").getJSONObject("1001002");
        redisUtil.set("observe", jsonObject);
        is.close();
        br.close();
        uRLConnection.disconnect();
    }
}
